'use strict';

/* Controllers */



function profileCtrl($scope, $http) {

  nexus.profileForm=$scope;
  var usrData;

  $http.get('myprofile').success(function(data) {
    $scope.user = data;
    $scope.selectedOptions = [];
     $.each(data.categories, function (i, cat) {
    	$scope.selectedOptions.push({text:cat});
	});
    $scope.editing = false;   
  });
  
  var categories = [];
  
  $http.get('category').success(function(data) {
	  $.each(data, function (i, cat) {
		  categories.push(cat.name);
	});
  });
  
  $scope.options = {
		    tags: categories
  };
  
  
  $scope.addEducation = function() {
	$scope.user.educationHistory.push({"collegeName":"","yearOfCompletion":"","educationType":"","id":null}); 

  };
  
  $scope.deleteEducation = function(index) {
	  $scope.user.educationHistory.splice(index,1);
  };
   $scope.addWork = function() {
	$scope.user.workHistory.push({"companyName":"","startDate":"","endDate":"","id":null});  

  };
  $scope.deleteWork = function(index) {
	  $scope.user.workHistory.splice(index,1);
  };
  
	$scope.update = function (){
	
	var reqData = $scope.user;
	var userCategories = [];
	 $.each($scope.selectedOptions, function (i, cat) {
		 userCategories.push(cat.text);
	  });
	reqData.categories = userCategories;
	
	var updatedUser = angular.toJson(reqData);
	
	$http.post('updateProfile', updatedUser).success(function(data) {
		$scope.editing = false;
	});
   
	};
  
}
profileCtrl.$inject = ['$scope','$http'];//inject required



